package.cpath = package.cpath .. ';C:/Users/win/AppData/Roaming/JetBrains/IdeaIC2020.3/plugins/intellij-emmylua/classes/debugger/emmy/windows/x86/?.dll'
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 9966)

--package.path = package.path .. ";../?.lua"
package.path = package.path .. ";C:/Users/win/Documents/_lua/util/?.lua"

local LinkedList = require("LinkedList")

function TestMoveBefore1()
    local list = LinkedList.new()

    local node1 = list:AddFirst(1)
    local node2 = list:AddLast(2)
    assert(2 == list:GetCount())

    local itr = list:GetIterator()
    assert(1 == itr():GetValue())
    assert(2 == itr():GetValue())
    assert(nil == itr())

    list:MoveBefore(node2, node1)
    assert(2 == list:GetCount())

    itr = list:GetIterator()
    assert(2 == itr():GetValue())
    assert(1 == itr():GetValue())
    assert(nil == itr())

    list:MoveBefore(node1, node2)
    assert(2 == list:GetCount())
    itr = list:GetIterator()
    assert(1 == itr():GetValue())
    assert(2 == itr():GetValue())
    assert(nil == itr())

    local node3 = list:AddLast(3)
    assert(3 == list:GetCount())

    list:MoveBefore(node2, node1) --中->头
    assert(3 == list:GetCount())
    itr = list:GetIterator()
    assert(2 == itr():GetValue())
    assert(1 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(nil == itr())

    list:MoveBefore(node3, node2) --尾->头
    assert(3 == list:GetCount())
    itr = list:GetIterator()
    assert(3 == itr():GetValue())
    assert(2 == itr():GetValue())
    assert(1 == itr():GetValue())
    assert(nil == itr())

    list:MoveBefore(node1, node2) --尾->中
    assert(3 == list:GetCount())
    itr = list:GetIterator()
    assert(3 == itr():GetValue())
    assert(1 == itr():GetValue())
    assert(2 == itr():GetValue())
    assert(nil == itr())
end
TestMoveBefore1()

function TestMoveAfter1()
    local list = LinkedList.new()

    local node1 = list:AddFirst(1)
    local node2 = list:AddLast(2)
    local node3 = list:AddLast(3)
    local node4 = list:AddLast(4)

    assert(4 == list:GetCount())
    local itr = list:GetIterator()
    assert(1 == itr():GetValue())
    assert(2 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(4 == itr():GetValue())

    list:MoveAfter(node1, node2) --头->中
    assert(4 == list:GetCount())
    itr = list:GetIterator()
    assert(2 == itr():GetValue())
    assert(1 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(4 == itr():GetValue())

    list:MoveAfter(node2, node4) --头->尾
    assert(4 == list:GetCount())
    itr = list:GetIterator()
    assert(1 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(4 == itr():GetValue())
    assert(2 == itr():GetValue())

    list:MoveAfter(node2, node3) --尾->中
    assert(4 == list:GetCount())
    itr = list:GetIterator()
    assert(1 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(2 == itr():GetValue())
    assert(4 == itr():GetValue())

    list:MoveAfter(node4, node1) --尾->头
    assert(4 == list:GetCount())
    itr = list:GetIterator()
    assert(1 == itr():GetValue())
    assert(4 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(2 == itr():GetValue())
end
TestMoveAfter1()

function TestRemove1()
    local list = LinkedList.new()

    local node1 = list:AddFirst(1)
    local node2 = list:AddLast(2)
    local node3 = list:AddLast(3)
    local node4 = list:AddLast(4)

    assert(4 == list:GetCount())
    local itr = list:GetIterator()
    assert(1 == itr():GetValue())
    assert(2 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(4 == itr():GetValue())

    list:RemoveFirst()
    assert(3 == list:GetCount())
    itr = list:GetIterator()
    assert(2 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(4 == itr():GetValue())
    assert(nil == itr())

    list:RemoveLast()
    assert(2 == list:GetCount())
    local itr = list:GetIterator()
    assert(2 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(nil == itr())

    list:Remove(node2)
    assert(1 == list:GetCount())
    local itr = list:GetIterator()
    assert(3 == itr():GetValue())
    assert(nil == itr())

    list:Remove(node3)
    assert(0 == list:GetCount())
    local itr = list:GetIterator()
    assert(nil == itr())

    node1 = list:AddLast(1)
    node2 = list:AddLast(2)
    node3 = list:AddLast(3)
    node4 = list:AddLast(4)
    assert(4 == list:GetCount())

    list:Remove(node2)
    assert(3 == list:GetCount())
    local itr = list:GetIterator()
    assert(1 == itr():GetValue())
    assert(3 == itr():GetValue())
    assert(4 == itr():GetValue())
    assert(nil == itr())

    list:Remove(node3)
    assert(2 == list:GetCount())
    local itr = list:GetIterator()
    assert(1 == itr():GetValue())
    assert(4 == itr():GetValue())
    assert(nil == itr())
end
TestRemove1()

